package com.hn.controller;

import com.hn.pojo.Dept;
import com.hn.service.DeptService;
import com.hn.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result selectAll(){
        List<Dept> data=deptService.selectAll();
        return Result.success(data);
    }
    //前端在http://localhost:8080/depts/19，在路径上写
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id){
        Integer i = deptService.deleteById(id);
        return i==1?Result.success():Result.error("该用户不存在");
    }
    //前端传来数据是json
    @PostMapping
    public Result addByName(@RequestBody Dept dept){
        deptService.addByName(dept);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable Integer id){
        List<Dept> date=deptService.selectById(id);
        return date!=null?Result.success(date):Result.error("该用户不存在");
    }
    @PutMapping
    public Result updateName(Dept dept){
        log.info("准备更新");
        deptService.updateName(dept);
        return Result.success();
    }
}
